package cn.yuanqiao.archive.storeRoom.service.impl;

import cn.yuanqiao.archive.appManager.domain.DataScopeBase;
import cn.yuanqiao.archive.storeRoom.domain.BoxRecord;
import cn.yuanqiao.archive.storeRoom.domain.Boxspec;
import cn.yuanqiao.archive.storeRoom.mapper.BoxRecordMapper;
import cn.yuanqiao.archive.storeRoom.mapper.BoxspecMapper;
import cn.yuanqiao.archive.storeRoom.service.IBoxRecordService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 档案盒记录Service业务层处理
 * 
 * @author xy
 * @date 2023-07-08
 */
@Service
public class BoxRecordServiceImpl implements IBoxRecordService
{
    @Autowired
    private BoxRecordMapper boxRecordMapper;

    @Autowired
    private BoxspecMapper boxspecMapper;

    /**
     * 查询档案盒记录
     * 
     * @param id 档案盒记录主键
     * @return 档案盒记录
     */
    @Override
    public BoxRecord selectBoxRecordById(Long id)
    {
        return boxRecordMapper.selectBoxRecordById(id,new BoxRecord().getTableName());
    }

    /**
     * 查询档案盒记录列表
     * 
     * @param boxRecord 档案盒记录
     * @return 档案盒记录
     */
    @Override
    public List<BoxRecord> selectBoxRecordList(BoxRecord boxRecord)
    {
        return boxRecordMapper.selectBoxRecordList(new DataScopeBase(),boxRecord);
    }

    /**
     * 新增档案盒记录
     * 
     * @param boxRecord 档案盒记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBoxRecord(BoxRecord boxRecord)
    {
        boxRecord.setIsDel(0);
        boxRecord.setCreator(SecurityUtils.getUserId());
        boxRecord.setCreateTime(DateUtils.getNowDate());
        Long specId=boxRecord.getSpecId();
        Boxspec boxspec1=boxspecMapper.selectBoxspecBySpecId(specId, SecurityUtils.getTenantCode().toUpperCase());
        //修改格子剩余个数
        Boxspec boxspec=new Boxspec();
        boxspec.setSpecId(specId);
        boxspec.setSpecCount((boxspec1.getSpecCount()==null?0:boxspec1.getSpecCount())+boxRecord.getBoxNum());
        boxspec.setSpecAcount((boxspec1.getSpecAcount()==null?0:boxspec1.getSpecAcount())+boxRecord.getBoxNum());
        boxspecMapper.updateBoxspec(boxspec);
        return boxRecordMapper.insertBoxRecord(boxRecord);
    }

    /**
     * 修改档案盒记录
     * 
     * @param boxRecord 档案盒记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBoxRecord(BoxRecord boxRecord)
    {
         BoxRecord boxRecord1=boxRecordMapper.selectBoxRecordById(boxRecord.getId(),new BoxRecord().getTableName());


        Long specId=boxRecord.getSpecId();

        Boxspec boxspec1=boxspecMapper.selectBoxspecBySpecId(specId, SecurityUtils.getTenantCode().toUpperCase());
        //修改格子剩余个数
        Boxspec boxspec=new Boxspec();
        boxspec.setSpecId(specId);
        boxspec.setSpecCount((boxspec1.getSpecCount()==null?0:boxspec1.getSpecCount())-boxRecord1.getBoxNum()+boxRecord.getBoxNum());
        boxspec.setSpecAcount((boxspec1.getSpecAcount()==null?0:boxspec1.getSpecAcount())-boxRecord1.getBoxNum()+boxRecord.getBoxNum());
        boxspecMapper.updateBoxspec(boxspec);

        return boxRecordMapper.updateBoxRecord(boxRecord);
    }

    /**
     * 批量删除档案盒记录
     * 
     * @param ids 需要删除的档案盒记录主键
     * @return 结果
     */
    @Override
    public int deleteBoxRecordByIds(Long[] ids)
    {
        List<Boxspec> boxList=boxspecMapper.selectBoxspecListByBox(new Boxspec());
        Boxspec boxspec=new Boxspec();
        for(int i=0;i<ids.length;i++){
            BoxRecord boxRecord=boxRecordMapper.selectBoxRecordById(ids[i],new BoxRecord().getTableName());
            for(Boxspec boxspec1:boxList){
                if(boxRecord.getSpecId().equals(boxspec1.getSpecId())){
                    boxspec.setSpecId(boxRecord.getSpecId());
                    boxspec.setSpecCount((boxspec1.getSpecCount()==null?0:boxspec1.getSpecCount())-boxRecord.getBoxNum());
                    boxspec.setSpecAcount((boxspec1.getSpecAcount()==null?0:boxspec1.getSpecAcount())-boxRecord.getBoxNum());
                    boxspecMapper.updateBoxspec(boxspec);
                }

            }

        }


        return boxRecordMapper.deleteBoxRecordByIds(ids,SecurityUtils.getTenantCode().toUpperCase());
    }

    /**
     * 删除档案盒记录信息
     * 
     * @param id 档案盒记录主键
     * @return 结果
     */
    @Override
    public int deleteBoxRecordById(Long id)
    {
        return boxRecordMapper.deleteBoxRecordById(id);
    }
}
